package com.julang.check.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.julang.check.bean.RepeatWord;
import com.julang.check.bean.RepeatWordAnalysis;
import com.julang.check.bean.ResponseEntity;
import com.julang.check.api.entity.YyReportAnalysis;
import com.julang.check.api.mapper.YyReportAnalysisMapper;
import com.julang.check.api.service.YyReportAnalysisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.julang.check.utils.CommonData.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 赵自强
 * @since 2019-06-05
 */
@Service
public class YyReportAnalysisServiceImpl extends ServiceImpl<YyReportAnalysisMapper, YyReportAnalysis> implements YyReportAnalysisService {

	public List<YyReportAnalysis> executeAnalysisSentence(String fileName, String id) {
		List<YyReportAnalysis> list = new ArrayList<>();
		Document document = null;
		try {
			document = Jsoup.parse(new File(fileName), "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		Elements elements = document.getElementsByClass("detail_tableText");
		int i = Integer.parseInt(id);
		for (Element element : elements) {
			Elements elements1 = element.children().select("tr");
			for (Element element1 : elements1) {
				Elements elements2 = element1.children().get(2).children().select("div.corrAdv_content").select(
						"span");
				String result = "";
				for (Element element2 : elements2) {
					result += element2.html();
				}
				list.add(new YyReportAnalysis(i,
						element1.children().get(0).children().get(0).children().get(2).html(), result));
			}
		}

		return list;
	}

	public List<YyReportAnalysis> getAnalysisDataByReportId(String id) {
		return this.list(new QueryWrapper<YyReportAnalysis>().lambda().eq(YyReportAnalysis::getReportId, id));
	}

	/**解析关键词的数量以及出现的次数，并且保存到本地*/
	public RepeatWordAnalysis getRepeatWordAnalysisData(List<YyReportAnalysis> list, String filePath) throws IOException {
		RepeatWordAnalysis resylt = new RepeatWordAnalysis();
		for (int i = 0; i < list.size(); i++) {
			String[] strings = list.get(i).getRepeatWords().split(REPEATWORDSPLITE);
			for (int i1 = 0; i1 < strings.length; i1++) {
				resylt.pushRepeatWord(strings[i1], String.valueOf(list.get(i).getId()));
			}
		}
		int a = resylt.saveToFile(filePath);
		System.out.println("保存了数据数量：" + a);
		System.out.println("地址：" + filePath);
		return resylt;
	}

	/**进行关键词的替换*/
	public List<ResponseEntity.WordReplaceResult> getReplaceResult(String reportId, RepeatWordAnalysis data) {
		List<YyReportAnalysis> list = this.getAnalysisDataByReportId(reportId);
		List<ResponseEntity.WordReplaceResult> resultList = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {
			YyReportAnalysis ye = list.get(i);
			String[] strings = ye.getRepeatWords().split(REPEATWORDSPLITE);
			String d = ye.getOriginSentence();
			System.out.println(d);
			for (int j = 0; j < strings.length; j++) {
				RepeatWord r = data.getRepeatWord(strings[j]);
				if (r == null || !r.isCanPlace()) {
					continue;
				}
				System.out.println(strings[j] + " 替换 " + r.getReplaceWord());
				d = d.replaceAll(IGNORESUPPERCASE + strings[j], GREENTAGSTART + r.getReplaceWord() + GREENTAGEND);
			}
			System.out.println("替换后 " + i + ": " + d);
			resultList.add(new ResponseEntity.WordReplaceResult(ye.getOriginSentence(), d, ye.getRepeatWords()));
		}
		return resultList;
	}
}
